package com.itheima.service.impl;

import com.itheima.mapper.EmpMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.*;
import com.itheima.service.ReportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liuyp
 * @since 2025/7/24
 */
@Service
public class ReportServiceImpl implements ReportService {
    @Resource
    private EmpMapper empMapper;
    @Resource
    private StudentMapper studentMapper;

    @Override
    public EmpJobData empJobData() {
        // 1. 调用mapper，执行统计SQL
        List<EmpJob> list = empMapper.empJobData();

        // 2. 处理数据：转换成客户端要求的结果，要什么给什么，要多少给多少
        // 2.1 从List<EmpJob>里取每个EmpJob的job值，得到List<职位>，可使用stream流式处理
        //      将流水线的每个元素 交给我们提供的函数，我们函数返回结果，  将函数返回值流转到下一环节
        List<String> jobList = list.stream().map(EmpJob::getJob).collect(Collectors.toList());
        // 2.2 从List<EmpJob>里每个EmpJob的cnt值，得到List<人数>
        List<Integer> dataList = list.stream().map(EmpJob::getCnt).collect(Collectors.toList());

        // 3. 封装结果并返回
        return new EmpJobData(jobList, dataList);
    }

    @Override
    public List<EmpGender> empGenderData() {
        List<EmpGender> list = empMapper.empGenderData();
        return list;
    }

    @Override
    public List<StudentDegree> studentDegreeData() {
        return studentMapper.studentDegreeData();
    }

    @Override
    public StudentCountData studentCountData() {
        //调用Mapper，统计每个班级的人数
        List<StudentCount> list = studentMapper.studentCountData();

        //转换数据
        List<String> classNameList = list.stream().map(StudentCount::getClazzName).collect(Collectors.toList());
        List<Integer> countList = list.stream().map(StudentCount::getCnt).collect(Collectors.toList());

        //封装结果
        return new StudentCountData(classNameList, countList);
    }
}
